home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Various / DevDisk 65 (1989)(DevWare PD).zip / DevDisk 65 (1989)(DevWare PD).adf / 3d / 3d.c next >
C/C++ Source or Header  |  1990-07-11  |  6KB  |  211 lines

  1. /* 3D library - By Steve Ludtke  */
  2. /* cre : 8/14/89   mod : 1/5/90  */
  3. /*           ver 1.0             */
  4. /*       Copyright 1990          */
  5.  
  6.  
  7. /*
  8.           The 3d library and all associated software in this distribution
  9.           is Copyright 1990 by Steven J. Ludtke. You have permission to
  10.           use and/or modify this code for any purpose commercial or non-
  11.           commercial with two conditions : I must be given credit in any
  12.           distributed product's documentation, and if any part of this
  13.           package is used in any commercial product (even Shareware) one
  14.           free copy of said software must be sent to me at the following
  15.           address : Steven Ludtke, 406 Yale Cir., Glenwood Springs, CO
  16.           81601; all other royalties are waived. This Copyright notice
  17.           must accompany any distributions of any part of this package,
  18.           and in general, the package should be distributed intact, with
  19.           no modifications. This notice must not be removed from the 3d.c,
  20.           test.c, and cube.c source code in this release.
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <exec/types.h>
  25. #include <exec/lists.h>
  26. #include <exec/memory.h>
  27. #include <graphics/gfx.h>
  28. #include <graphics/rastport.h>
  29. #include <graphics/gfxbase.h>
  30. #include <graphics/gfxmacros.h>
  31. #include <graphics/text.h>
  32. #define INTUITION_INTUITIONBASE_H TRUE
  33. #include <intuition/intuition.h>
  34. #include<math.h>
  35.  
  36. #define PI      3.141592
  37.  
  38. typedef struct { long *x,*y,*z; long *tx,*ty,*tz;} VECTOR;
  39.  
  40. /* this structure is used to sort surfaces for hidden line removal */
  41. typedef struct { long d,n; } SORTS;
  42.  
  43. typedef struct { unsigned short l; unsigned short nl : 1;
  44.     unsigned short nc : 1; unsigned short nco : 1; } LINES;
  45.  
  46. long xfm[3][4];              /* transformation matrix */
  47. struct TmpRas TR;            /* TmpRas and AreaInfo for filled polygons */
  48. struct AreaInfo AI;
  49. WORD LB[500];                /* Line buffer. Polygons limited to 100 lines */
  50.  
  51. /* comparison routine for qsort() */
  52. cmp(x,y)
  53. SORTS *x,*y;
  54. {
  55. return(y->d - x->d);
  56. }
  57.  
  58. /* set new x-form matrix  a1=x/y  a2=z/xy  a3=z'/x'y' (see Goldstein 146) */
  59. /* tx,ty,tz are translsations after rotation                               */
  60. setxfm(a1,a2,a3,tx,ty,tz,n,d)
  61. double a1,a2,a3;
  62. long tx,ty,tz,n,d;
  63. {
  64. xfm[0][0]=(long) (( cos(a3)*cos(a1)-cos(a2)*sin(a1)*sin(a3))*REZ)*n/d;
  65. xfm[1][0]=(long) ((-sin(a3)*cos(a1)-cos(a2)*sin(a1)*cos(a3))*REZ)*n/d;
  66. xfm[2][0]=(long) ( sin(a2)*sin(a1)*REZ)*n/d;
  67. xfm[0][1]=(long) (( cos(a3)*sin(a1)+cos(a2)*cos(a1)*sin(a3))*REZ)*n/d;
  68. xfm[1][1]=(long) ((-sin(a3)*sin(a1)+cos(a2)*cos(a1)*cos(a3))*REZ)*n/d;
  69. xfm[2][1]=(long) (-sin(a2)*cos(a1)*REZ)*n/d;
  70. xfm[0][2]=(long) ( sin(a3)*sin(a2)*REZ)*n/d;
  71. xfm[1][2]=(long) ( cos(a3)*sin(a2)*REZ)*n/d;
  72. xfm[2][2]=(long) ( cos(a2)*REZ)*n/d;
  73. xfm[0][3]=tx;
  74. xfm[1][3]=ty;
  75. xfm[2][3]=tz;
  76. return(0);
  77. }
  78.  
  79. /* rotate without perspective */
  80. rotate(v,n)
  81. VECTOR *v;
  82. int n;
  83. {
  84. int i;
  85. for (i=0; i<n; i++) {
  86. v->ty[i]=(v->x[i]*xfm[1][0]+v->y[i]*xfm[1][1]+v->z[i]*xfm[1][2])/REZ+xfm[1][3];
  87. v->tx[i]=((v->x[i]*xfm[0][0]+v->y[i]*xfm[0][1]+v->z[i]*xfm[0][2])/REZ+xfm[0][3])*ASPECT+XCEN;
  88. v->tz[i]=((v->x[i]*xfm[2][0]+v->y[i]*xfm[2][1]+v->z[i]*xfm[2][2])/REZ+xfm[2][3])+YCEN;
  89. }
  90. return(0);
  91. }
  92.  
  93. /* rotate with perspective */
  94. rotatev(v,n)
  95. VECTOR *v;
  96. int n;
  97. {
  98. long i,j;
  99. for (i=0; i<n; i++) {
  100. if (0==(v->ty[i]=(v->x[i]*xfm[1][0]+v->y[i]*xfm[1][1]+v->z[i]*xfm[1][2])/REZ+xfm[1][3])) v->ty[i]= -1;
  101. j=D3VDIST- v->ty[i];
  102. if (j<=0) j=1000000L;
  103. v->tx[i]=((v->x[i]*xfm[0][0]+v->y[i]*xfm[0][1]+v->z[i]*xfm[0][2])/REZ+xfm[0][3])*j/D3VDIST*ASPECT+XCEN;
  104. v->tz[i]=((v->x[i]*xfm[2][0]+v->y[i]*xfm[2][1]+v->z[i]*xfm[2][2])/REZ+xfm[2][3])*j/D3VDIST+YCEN;
  105. }
  106. return(0);
  107. }
  108.  
  109. /* set up 1 or 2 rastports for filled shape drawing */
  110. Init3Ras(rp,rp2)
  111. struct RastPort *rp,*rp2;
  112. {
  113. PLANEPTR buf;
  114. InitArea(&AI,&LB,200);
  115. rp->AreaInfo=&AI;
  116. if (rp2!=NULL) rp2->AreaInfo=&AI;
  117. buf=(PLANEPTR)AllocRaster(XHI,YHI);
  118. InitTmpRas(&TR,buf,RASSIZE(XHI,YHI));
  119. rp->TmpRas=&TR;
  120. if (rp2!=NULL) rp2->TmpRas=&TR;
  121. return(0);
  122. }
  123.  
  124. /* free memory allocated by Init3Ras */
  125. Exit3d(rp)
  126. struct RastPort *rp;
  127. {
  128. FreeRaster(rp->TmpRas->RasPtr,XHI,YHI);
  129. return(0);
  130. }
  131.  
  132. /* draw outlines of 3d shapes */
  133. d3lines(v,l,n,rp)
  134. VECTOR *v;
  135. int n;
  136. LINES *l;
  137. struct RastPort *rp;
  138. {
  139. int i,flag;
  140.  
  141. flag=0;
  142. for (i=0; i<n; i++) {
  143.  
  144. while (l[i].nc || l[i].nco) {                    /* change colors if necc. */
  145.     if (l[i].nco) SetAPen(rp,l[i].l);
  146.     i++;
  147.     }
  148. if (v->tx[l[i].l]>XLO && v->tx[l[i].l]<XHI &&    /* clip to defined region */
  149.     v->tz[l[i].l]>YLO && v->tz[l[i].l]<YHI ) {
  150. if (l[i].nl) { Move(rp,v->tx[l[i].l],v->tz[l[i].l]); flag=0; }
  151. else {
  152.     if (v->ty[l[i].l]<0) flag=1;
  153.     else if (flag==1) { Move(rp,v->tx[l[i].l],v->tz[l[i].l]); flag=0; }
  154.     else Draw(rp,v->tx[l[i].l],v->tz[l[i].l]);
  155.     }
  156. }
  157. else flag=1;
  158.  
  159. }
  160. return(0);
  161. }
  162.  
  163. /* draw filled 3d shapes with hidden lie removal */
  164. d3surf(v,l,n,rp)
  165. VECTOR *v;
  166. LINES *l;
  167. int n;
  168. struct RastPort *rp;
  169. {
  170. int i,j,flag,c;
  171. int cmp();
  172. SORTS s[100];
  173.  
  174. c= -1;
  175. for (i=0; i<n; i++) {                                /* set up SORTS for */
  176.     if (l[i].nl)  { c++; s[c].n=i; s[c].d=0; }       /* qsort, HLR       */
  177.     if (!l[i].nc && !l[i].nco) s[c].d+=v->ty[l[i].l];
  178.     }
  179. qsort(s,c+1,sizeof(SORTS),cmp);                    /* sort lines for HLR */
  180.  
  181. for (j=0; j<c+1; j++) {                            /* draw shapes in order */
  182. flag=0;
  183. i=s[j].n;
  184. while(!l[i].nl && i!=n || i==s[j].n) {
  185. while (l[i].nc || l[i].nco) {
  186.     if (l[i].nc) SetAPen(rp,l[i].l);
  187.     if (l[i].nco) SetOPen(rp,l[i].l);
  188.     i++;
  189.     }
  190. if (v->tx[l[i].l]>XLO && v->tx[l[i].l]<XHI &&
  191.     v->tz[l[i].l]>YLO && v->tz[l[i].l]<YHI ) {
  192. if (l[i].nl) { AreaMove(rp,v->tx[l[i].l],v->tz[l[i].l]); flag=0; }
  193. else {
  194.     if (v->ty[l[i].l]<0) flag=1;
  195.     else if (flag==1) { AreaMove(rp,v->tx[l[i].l],v->tz[l[i].l]); flag=0; }
  196.     else AreaDraw(rp,v->tx[l[i].l],v->tz[l[i].l]);
  197.     }
  198. }
  199. else flag=1;
  200. i++;
  201. }
  202. AreaEnd(rp);
  203.  
  204. }
  205. return(0);
  206. }
  207.  
  208.  
  209.  
  210.  
  211.